package com.example.car;


import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.*;
import com.huaweicloud.sdk.iotda.v5.auth.IoTDACredentials;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.huaweicloud.sdk.iotda.v5.model.*;

public class ListDevicesSolution {

    // REGION_ID：如果是上海一，请填写"cn-east-3"；如果是北京四，请填写"cn-north-4";如果是华南广州，请填写"cn-south-4"
    private static final String REGION_ID = "cn-north-4";
    // ENDPOINT：请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
    private static final String ENDPOINT = "https://02e79f0fe2.iotda.cn-north-4.myhuaweicloud.com";

    // 标准版/企业版：需自行创建Region对象
   // public static final Region REGION_CN_NORTH_4 = new Region(REGION_ID, ENDPOINT);


    public static void main(String[] args) {
        String ak = "FRGD896V6XFNCRAVC4AM";
        String sk = "2THTLrdVT5vpx8pxXsOKRbVfORU2D7GOlfbmDk4i";
        String projectId = "62b89d894c7c4e3646b7bf3e";

        // 创建认证
        ICredential auth = new IoTDACredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                // 基础版：请选择IoTDARegion中的Region对象
                .withRegion(IoTDARegion.CN_NORTH_4)
                // 标准版/企业版：需自行创建Region对象
                //.withRegion(new Region(REGION_ID, ENDPOINT))
                .build();
        // 实例化请求对象
        ListDevicesRequest request = new ListDevicesRequest();
        try {
            // 调用查询设备列表接口
            ListDevicesResponse response = client.listDevices(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}